//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Created   : 08/05/2005
// Last Modif: 09/05/2005
//



var MOD_CST = {
  currver:"",
  impver:1
}

MOD_CST.showtxtallclaims=function() {
  SetClaimToolEditSectionExtra(MOD_CST.textbar(html_tabs_obj_selected("viewvers")));
  /*
  if(document.getElementById("viewversup")) {
    document.getElementById("viewversup").innerHTML=MOD_CST.textbar(html_tabs_obj_selected("viewvers"));
    document.getElementById("viewverpages").innerHTML=html_pages("edit_claimsetview()");
    }*/
  return showtxtallclaims();
} 
MOD_CST.cltree=function() {
  SetClaimToolEditSectionExtra("");
  /*
  if(document.getElementById("viewversup")) {
    document.getElementById("viewversup").innerHTML="";
    document.getElementById("viewverpages").innerHTML="";
    }*/
  return cltree();
}
MOD_CST.showdiffallclaims=function() {
  SetClaimToolEditSectionExtra(MOD_CST.diffbar(html_tabs_obj_selected("viewvers")));
  /*
  if(document.getElementById("viewversup")) {
    document.getElementById("viewversup").innerHTML=MOD_CST.diffbar(html_tabs_obj_selected("viewvers"));
    document.getElementById("viewverpages").innerHTML=html_pages("edit_claimsetview()");
    }*/
  return showdiffallclaims();
} 


// Tab declaration 
MOD_CST.tabobjs=[];
MOD_CST.tabobjs["Text"]={
  callback: MOD_CST.showtxtallclaims,
  yeslabel:"<TABLE width=100%><TR width=100%><TD class='fonttabsyes' align=left>"+
  "Text</TD><TD align=right><button onClick='do_import();'>"+
  html_img(import_img)+"</button></TD><TR></TABLE>"
};
MOD_CST.tabobjs["Tree"]={
  callback:MOD_CST.cltree
};
MOD_CST.tabobjs["Diff"]={
  callback:MOD_CST.showdiffallclaims
};


function do_import() {
  var ver=GetClaimSetVersion();
  if(ver) {
    import_claim_text_fromfile(GetDossierLang());
    edit_claimsetview();
  } else {
    alert("Cannot find current version.");
  }
}  


function activate_claimset(vers) {
  SelectClaimSet(vers);  
  SetPrefVersion(GetDossierNumber(),vers);
}

function open_claimset(vers) {
  activate_claimset(vers);
  edit_claimsetview(vers);
}



function importall() {
  MOD_CST.impver=html_selected_iter("impver",MOD_CST.impver);
  if(confirm("Import all claims from version "+MOD_CST.impver+" ?")) {
    CloneClaimSet(GetClaimSetVersion(),MOD_CST.impver);
    edit_claimsetview();
  }
}


MOD_CST.onchangerefver=function() {
  SetRefDiff(html_selected_iter("diffver"));
  edit_claimsetview();
}

MOD_CST.onchangeimpver=function(){
  MOD_CST.impver=html_selected_iter("impver");
}

MOD_CST.diffbar=function(vers) {
  var html="<fieldset><legend>Compare</legend>with set #";
  //  html+=html_selection_iter("diffver",new range_iterator(1,vers),GetRefDiff(),"MOD_CST.onchangerefver()");
  html+=html_selection_iter("diffver",new range_iterator(1,GetClaimSetsLength()),GetRefDiff(),"MOD_CST.onchangerefver()");
  html+="</fieldset>";
  return html;
} 

MOD_CST.textbar=function(vers) {
  var html="";
  if(vers > 1) {
    MOD_CST.impver   =html_selected_iter("impver",MOD_CST.impver);
    html+="<fieldset><legend>Import</legend>from set #";
    html+=html_selection_iter("impver",new range_iterator(1,vers-1),
				    MOD_CST.impver,
				    "MOD_CST.onchangeimpver()");
    html+=" <input type=button onclick='importall();' value='All' align=right/></fieldset>";
  } 
  return html;
}


// Tab declaration 
MOD_CST.tabobjs=[];
MOD_CST.tabobjs["Text"]={
  callback: MOD_CST.showtxtallclaims,
  yeslabel:"<TABLE width=100%><TR width=100%><TD class='fonttabsyes' align=left>"+
  "Text</TD><TD align=right><button onClick='do_import();'>"+
  html_img(import_img)+"</button></TD><TR></TABLE>"
};
MOD_CST.tabobjs["Tree"]={
  callback:MOD_CST.cltree
};
MOD_CST.tabobjs["Diff"]={
  callback:MOD_CST.showdiffallclaims
};



MOD_CST._edit_claimsetviewvers=function() {
  var vers=html_tabs_obj_selected("viewvers") || GetClaimSetVersion(); 
  SelectClaimSet(vers);
  ClaimToolTitle();
  return _edit_claimsetview(vers);
}

function _edit_claimsetview(vers) {
  var impbar="",pagebar="";
  if(!vers) {
    vers=GetClaimSetVersion();
  }
  var subsection=html_tabs_obj_selected("subviewvers"+vers)|| "Text";
  switch(subsection) {
  case "Diff":  
    impbar=MOD_CST.diffbar(vers);
    //pagebar=html_pages("edit_claimsetview()");
    break;
  case "Text":
    impbar=MOD_CST.textbar(vers);
    // pagebar=html_pages("edit_claimsetview()");
  }
  return ClaimToolEditSubSectionHtml(subsection,
				     html_tabs_obj("subviewvers"+vers,MOD_CST.tabobjs),
				     "edit_claimsetview()",
				     impbar);
}

MOD_CST._edit_claimsetviewadd=function() {
  return MOD_DOSSIER._editclaimsets(function() { return edit_claimsetview("Add")});
}


MOD_CST.section={
  title:"Edit Claims",
  no_info:"yes",
  no_filter:"yes",
  callback:edit_claimsetview,
  render:"edit_claimsetview()"
};

function edit_claimsetview(i_vers) {
  if(!CLAIMSET) {
    alert("Create or Select a claim set first !");
    return;
  }
  MOD_CST.vertabobjs=[];
  var cls_iter= new claimsets_iterator();
  while(!cls_iter.end()) {
    var v=cls_iter.getValue();
    MOD_CST.vertabobjs[v]={
      render:_edit_claimsetview,
      callback:MOD_CST._edit_claimsetviewvers,
      label:"#"+v+": "+GetClaimSetLabel(v)+"<BR>"+GetClaimSetDate(v)
    }
    cls_iter.next();
  }
  MOD_CST.vertabobjs["Add"]={
    callback:MOD_CST._edit_claimsetviewadd,
    label:"<TABLE width=100%><TR><TD align=center>"+html_img(add_img)+"<TD></TR></TABLE>"
  }
  i_vers = i_vers || GetClaimSetVersion();

  /*
  ClaimToolSectionObj(MOD_CST.section,  html_tabs_obj("viewvers",
						      MOD_CST.vertabobjs,
						      i_vers));
  */
  ClaimToolEditSubSection("Edit Claims",
		       html_tabs_obj("viewvers",
				     MOD_CST.vertabobjs,
				     i_vers),
		       "edit_claimsetview()");
}



function add_claimset(clsdate,clsphase,appnum,nbclaims,label) {
  //alert("nbclaims "+nbclaims);
  var clsnum=GetDossierNumber();
  
  var vers=GetNextVersionNumber();
  
  AddClaimSet(appnum,clsdate,clsphase,nbclaims,label);

  CT_show_menu("Edit");
  CT_show_menu("Comms");
  CT_show_menu("Tools");
  html_reset();
  SelectClaimSet(vers);
  //html_tabs_doselect("clsview","Text");
  
  SetFilter("All");
  InitPageClaimIterator("",vers);
  
  edit_claimsetview(vers);
}
